package packet1;

import java.util.ArrayList;
import java.util.List;

public class twofour {
    static final double TARGET = 24;
    static final double EPISLON = 1e-6;
    public static void main(String[] args) {
        System.out.println(judgePoint24(new int[]{1,2,1,7}));
    }
    public static boolean judgePoint24(int[] cards) {
        return helper(new double[]{ cards[0], cards[1], cards[2], cards[3] });
    }
    public static boolean helper(double[] nums){
        if(nums.length == 1)
            return Math.abs(nums[0] - TARGET) < EPISLON;
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                double[] temp = new double[nums.length - 1];
                for (int k = 0, t = 0; k < nums.length; k++) {
                    if(k != i && k != j){
                        temp[t++] = nums[k];
                    }
                }
                for(double nu : Cal(nums[i], nums[j])) {
                    temp[temp.length - 1] = nu;
                    if (helper(temp))
                        return true;
                }
            }
        }
        return false;
    }
    public static List<Double> Cal(double a, double b){
        List<Double> re = new ArrayList<>();
        re.add(a + b);
        re.add(a - b);
        re.add(b - a);
        re.add(a * b);
        if (!(Math.abs(b) < EPISLON))
            re.add(a / b);
        if (!(Math.abs(a) < EPISLON))
            re.add(b / a);
        return re;
    }

}
